<template>
  <div id="myCard">
    <div class="top">
      <div class="userIcon">
        <img
          src="https://tsyx-rights-and-interests.oss-cn-hangzhou.aliyuncs.com/icon/petroleum.png"
          alt=""
        />
      </div>
      <div class="userName">
        <p class="name">{{ userMsg.nickName }}</p>
        <p class="tel">电话 :{{ userMsg.phoneNumber }}</p>
      </div>
      <div class="userCardNum" v-if="status == 2">
        <h3 class="num">{{ allNum }}</h3>
        <p>未使用</p>
      </div>
    </div>
    <van-tabs v-model="status">
      <van-tab title="未使用" name="2">
        <ul class="checkQuan">
          <li
            :class="active == 1 ? 'activeCheckQuan' : ''"
            @click="checkQuanClick('1')"
          >
            汽油券
          </li>
          <li
            :class="active == 2 ? 'activeCheckQuan' : ''"
            @click="checkQuanClick('2')"
          >
            机油券
          </li>
          <li
            :class="active == 3 ? 'activeCheckQuan' : ''"
            @click="checkQuanClick('3')"
          >
            洗车券
          </li>
          <li
            :class="active == 4 ? 'activeCheckQuan' : ''"
            @click="checkQuanClick('4')"
          >
            格桑泉
          </li>
        </ul>
      </van-tab>
      <van-tab title="已使用" name="3">
        <ul class="checkQuan">
          <li
            :class="active == 1 ? 'activeCheckQuan' : ''"
            @click="checkQuanClick('1')"
          >
            汽油券
          </li>
          <li
            :class="active == 2 ? 'activeCheckQuan' : ''"
            @click="checkQuanClick('2')"
          >
            机油券
          </li>
          <li
            :class="active == 3 ? 'activeCheckQuan' : ''"
            @click="checkQuanClick('3')"
          >
            洗车券
          </li>
          <li
            :class="active == 4 ? 'activeCheckQuan' : ''"
            @click="checkQuanClick('4')"
          >
            格桑泉
          </li>
        </ul>
      </van-tab>
      <van-tab title="已过期" name="5">
        <ul class="checkQuan">
          <li
            :class="active == 1 ? 'activeCheckQuan' : ''"
            @click="checkQuanClick('1')"
          >
            汽油券
          </li>
          <li
            :class="active == 2 ? 'activeCheckQuan' : ''"
            @click="checkQuanClick('2')"
          >
            机油券
          </li>
          <li
            :class="active == 3 ? 'activeCheckQuan' : ''"
            @click="checkQuanClick('3')"
          >
            洗车券
          </li>
          <li
            :class="active == 4 ? 'activeCheckQuan' : ''"
            @click="checkQuanClick('4')"
          >
            格桑泉
          </li>
        </ul>
      </van-tab>
    </van-tabs>
    <!-- <van-tabs v-model="active" type="card" style="background-color: #f7f8fa;border: none">
      <van-tab title="汽油券" name="1" style="border-radius: .3rem">
        
      </van-tab>
      <van-tab title="机油券" name="2">
        <div class="rouleBox">
          <img
            src="https://tsyx-rights-and-interests.oss-cn-hangzhou.aliyuncs.com/img/cardRule.png"
            alt=""
            class="roul"
          />
          <div class="toRouleMsg" @click="toMsg"></div>
        </div>
        <div
          class="quan"
          v-for="(item, index) in list"
          :key="index"
          @click="toQuan(item)"
        >
          <div class="leftBox">
            <span>￥</span>
            <h3 class="money">{{ item.couponAmount }}</h3>
            <p>优惠券</p>
          </div>
          <div class="midBox">
            <h4>{{ item.couponName }}</h4>
            <p>{{ item.remarks }}</p>
            <div>有效使期至：{{ item.expiredDate }}</div>
          </div>
          <div class="rightBox">
            <div class="bottomToMsgQuan">扫码使用</div>
          </div>
        </div>
      </van-tab>
      <van-tab title="洗车券" name="3">
        <div class="rouleBox">
          <img
            src="https://tsyx-rights-and-interests.oss-cn-hangzhou.aliyuncs.com/img/cardRule.png"
            alt=""
            class="roul"
          />
          <div class="toRouleMsg" @click="toMsg"></div>
        </div>
        <div
          class="quan"
          v-for="(item, index) in list"
          :key="index"
          @click="toQuan(item)"
        >
          <div class="leftBox">
            <span>￥</span>
            <h3 class="money">{{ item.couponAmount }}</h3>
            <p>优惠券</p>
          </div>
          <div class="midBox">
            <h4>{{ item.couponName }}</h4>
            <p>{{ item.remarks }}</p>
            <div>有效使期至：{{ item.expiredDate }}</div>
          </div>
          <div class="rightBox">
            <div class="bottomToMsgQuan">扫码使用</div>
          </div>
        </div>
      </van-tab>
      <van-tab title="格桑泉" name="4">
        <div class="rouleBox">
          <img
            src="https://tsyx-rights-and-interests.oss-cn-hangzhou.aliyuncs.com/img/cardRule.png"
            alt=""
            class="roul"
            @click="toRoule(2)"
          />
          <div class="toRouleMsg" @click="toMsg"></div>
        </div>
        <div
          class="quan"
          v-for="(item, index) in list"
          :key="index"
          @click="toQuan(item)"
        >
          <div class="leftBox">
            <span>￥</span>
            <h3 class="money">{{ item.couponAmount }}</h3>
            <p>优惠券</p>
          </div>
          <div class="midBox">
            <h4>{{ item.couponName }}</h4>
            <p>{{ item.remarks }}</p>
            <div>有效使期至：{{ item.expiredDate }}</div>
          </div>
          <div class="rightBox">
            <div class="bottomToMsgQuan">扫码使用</div>
          </div>
        </div>
      </van-tab>
    </van-tabs> -->
    <div class="rouleBox">
      <img
        src="https://tsyx-rights-and-interests.oss-cn-hangzhou.aliyuncs.com/img/cardRule.png"
        alt=""
        class="roul"
      />
      <div class="toRouleMsg" @click="toRoule"></div>
    </div>

    <div
      class="quan"
      v-for="(item, index) in list"
      :key="index"
      @click="toQuan(item)"
    >
      <div class="leftBox">
        <span>￥</span>
        <h3 class="money">{{ item.couponAmount }}</h3>
        <p>优惠券</p>
      </div>
      <div class="midBox">
        <h4>{{ item.couponName }}</h4>
        <p>{{ item.remarks }}</p>
        <div>有效使期至：{{ item.expiredDate }}</div>
      </div>
      <div class="rightBox">
        <div class="bottomToMsgQuan">扫码使用</div>
      </div>
    </div>
  </div>
</template>

<script>
import req from "@/utils/index";
import { Toast } from "vant";
export default {
  data() {
    return {
      active: 1,
      status: "3",
      list: [],
      userMsg: {
        nickName: "",
        phoneNumber: "",
      },
      allNum: 0,
    };
  },
  methods: {
    getList(status, type) {
      req(
        "get",
        `/qy/user/coupon?pageNum=1&pageSize=999&status=${status}&type=${type}`,
        {}
      ).then((res) => {
        if (res.rspCode == "000000") {
          this.list = res.data;
        }
      });
    },
    getAllNum(type) {
      req("get", `/qy/user/coupon/num?type=${type}`, {}).then((res) => {
        if (res.rspCode == "000000") {
          this.allNum = res.data.unused;
        }
      });
    },
    // 去券
    toQuan(item) {
      if (this.status != 2) {
        Toast("已过期和已使用的券不可以再查看喔~");
        return;
      }
      this.$router.push({
        name: "quan",
        params: { item: JSON.stringify(item) },
      });
    },
    toRoule() {
      let id;
      if (this.active == 1) {
        id = 1;
      } else if (this.active == 4) {
        id = 2;
      } else {
        return;
      }
      this.$router.push({
        name: "roule",
        query: {
          id,
        },
      });
    },
    checkQuanClick(id) {
      this.active = id;
    },
  },
  created() {
    console.log(this.$route.params.id);
    this.status = this.$route.params.id || this.$store.state.myCardStatus;
    this.getList(this.status, this.active);
    this.getAllNum(this.active);
    this.userMsg = JSON.parse(window.localStorage.getItem("userMsg"));
  },
  watch: {
    active() {
      this.getList(this.status, this.active);
      this.getAllNum(this.active);
    },
    status(){
      this.getList(this.status, this.active);
      this.getAllNum(this.active);
    }
  },
};
</script>

<style lang='scss' scoped>
h1,
h2,
h3,
h4,
h5,
h6,
p {
  margin: 0;
  padding: 0;
}
#myCard {
  padding-bottom: 3rem;
}
.top {
  position: relative;
  width: 90%;
  height: 2.3rem;
  background-image: url(https://tsyx-rights-and-interests.oss-cn-hangzhou.aliyuncs.com/background/orderPayBG.png);
  background-size: 100%;
  background-repeat: no-repeat;
  color: #fff;
  margin: 1rem auto;
  .userIcon {
    position: absolute;
    left: 0.3rem;
    top: 0.3rem;
    width: 20%;
    img {
      width: 100%;
    }
  }
  .userName {
    position: absolute;
    left: 25%;
    top: 0.3rem;
    width: 35%;
    overflow: hidden;
    p {
      margin: 0;
      text-overflow: ellipsis;
      white-space: nowrap;
      overflow: hidden;
    }
    .name {
      font-size: 0.5rem;
      margin-top: 0.2rem;
    }
    .tel {
      font-size: 0.3rem;
      border-radius: 0.3rem;
      background-color: rgba(0, 0, 0, 0.3);
      padding: 0 0.1rem;
    }
  }
  .userCardNum {
    position: absolute;
    top: 50%;
    right: 1rem;
    transform: translateY(-50%);
    .num,
    p {
      text-align: center;
      margin: 0;
      padding: 0;
    }
    .num {
      font-size: 0.6rem;
      font-family: Helvetica;
    }
    p {
      font-family: PingFangSC, PingFangSC-Regular;
      font-weight: 400;
    }
  }
}
.rouleBox {
  position: relative;
  top: 0.5rem;
  text-align: center;
  margin-bottom: 1rem;
  .roul {
    width: 90%;
  }
  .toRouleMsg {
    position: absolute;
    top: 0.5rem;
    right: 0.5rem;
    width: 30%;
    height: 0.3rem;
    //   background-color: red;
  }
}
.quan {
  position: relative;
  width: 90%;
  height: 3rem;
  margin: 0.5rem auto;
  background-color: #fff;
  .leftBox {
    float: left;
    width: 25%;
    height: 3rem;
    text-align: center;
    padding-top: 0.8rem;
    box-sizing: border-box;
    color: #dc4433;
    .money {
      display: inline-block;
      margin: 0;
      padding: 0;
      font-size: 0.6rem;
    }
    p {
      margin: 0.1rem 0;
      padding: 0;
    }
  }
  .midBox {
    float: left;
    height: 3rem;
    width: 50%;
    text-align: left;
    padding-left: 0.4rem;
    box-sizing: border-box;
    h4 {
      margin: 0.7rem 0 0.1rem 0;
      font-size: 0.4rem;
    }
    p {
      margin: 0 0 0.1rem 0;
      font-size: 0.35rem;
      color: #dc4433;
    }
    div {
      color: #999;
      font-size: 10px;
    }
  }
  .rightBox {
    position: relative;
    float: left;
    width: 25%;
    height: 3rem;
    .bottomToMsgQuan {
      position: absolute;
      width: 60%;
      left: 50%;
      top: 50%;
      transform: translateX(-50%) translateY(-50%);
      padding: 0.3rem 0.2rem;
      background: linear-gradient(111deg, #cd2136 5%, #9e1129 88%);
      border-radius: 0.25rem;
      color: #feffff;
      text-align: center;
    }
  }
}
.checkQuan {
  width: 90%;
  margin: 0.3rem auto;
  overflow: hidden;
  li {
    float: left;
    padding: 0.1rem 0.3rem;
    border: 1px solid #cccccc;
    margin-right: 0.3rem;
    border-radius: 0.4rem;
    color: #666666;
  }
  .activeCheckQuan {
    border: 1px solid #dc4433;
    color: #ce1b23;
  }
}
</style>